package com.whccb.apps.office.document.at;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.commons.database.RowMapper;
import com.actionsoft.bpms.util.DBSql;

/**
 * 获取公文归档知识库纬度
 * 
 * @author xqli
 * 
 */
public class GetKmsLat extends AbstExpression {
	public GetKmsLat(ExpressionContext atContext, String expressionValue) {
		super(atContext, expressionValue);
	}

	public String execute(String expression) {
		String year = getParameter(expression, 1).trim();// 年度
		String kmsMemo = getParameter(expression, 2).trim();// 知识纬度描述
		String result = "";
		if (StringUtils.isNotEmpty(kmsMemo)) {
			String queryKmsLatSql = " SELECT ID,DIMENSIONNAME,PARENTID,MEMO FROM APP_ACT_KMS_DIMENSION WHERE ISENABLED = :ISENABLED  AND MEMO LIKE '%"
					+ kmsMemo + "%' ";
			Map<String, Object> kmsLatParam = new HashMap<String, Object>();
			kmsLatParam.put("ISENABLED", "1");
			List<Map<String, String>> kmsLatList = DBSql.query(queryKmsLatSql,
					kmsLatParam, new RowMapper<Map<String, String>>() {
						@Override
						public Map<String, String> mapRow(ResultSet rs,
								int index) throws SQLException {
							Map<String, String> map = new HashMap<String, String>();
							map.put("ID", rs.getString("ID"));
							map.put("DIMENSIONNAME",
									rs.getString("DIMENSIONNAME"));
							map.put("PARENTID", rs.getString("PARENTID"));
							map.put("MEMO", rs.getString("MEMO"));
							return map;
						}
					});
			// result = getRightKmsLat(year, kmsLatList);
			// 为了后期维护方便，经过商量，决定去掉知识库纬度里的年度相关的纬度，所以此处，做修改
			result = getRightKmsLat("", kmsLatList);
		}
		return result;
	}

	private String getRightKmsLat(String year,
			List<Map<String, String>> kmsLatList) {
		String result = "";
		if (kmsLatList != null && !kmsLatList.isEmpty()
				&& kmsLatList.size() > 0) {
			for (Map<String, String> kmsLatMap : kmsLatList) {
				if (kmsLatMap != null && !kmsLatMap.isEmpty()) {
					List<Map<String, String>> parentLatList = new ArrayList<Map<String, String>>();
					parentLatList = getParentKmsLat(parentLatList, kmsLatMap);
					if (parentLatList != null && !parentLatList.isEmpty()
							&& parentLatList.size() > 0) {
						for (Map<String, String> parentLatMap : parentLatList) {
							if (parentLatMap != null && !parentLatMap.isEmpty()) {
								if (year != null
										&& StringUtils.isNotEmpty(year)) {
									if (year.toString().equals(
											parentLatMap.get("DIMENSIONNAME"))) {
										result = result + kmsLatMap.get("ID")
												+ ",";
										break;
									}
								} else {
									result = result + kmsLatMap.get("ID") + ",";
									break;
								}
							}
						}
					}
				}
			}
		}
		return result;
	}

	private List<Map<String, String>> getParentKmsLat(
			List<Map<String, String>> resultList, Map<String, String> kmsLatMap) {
		if (kmsLatMap != null && !kmsLatMap.isEmpty()) {
			resultList.add(kmsLatMap);
			String parentId = kmsLatMap.get("PARENTID");
			if (StringUtils.isEmpty(parentId)) {
				return resultList;
			} else {
				String queryKmsLatSql = " SELECT ID,DIMENSIONNAME,PARENTID,MEMO FROM APP_ACT_KMS_DIMENSION WHERE ID = :ID ";
				Map<String, Object> kmsLatParam = new HashMap<String, Object>();
				kmsLatParam.put("ID", parentId);
				List<Map<String, String>> kmsLatList = DBSql.query(
						queryKmsLatSql, kmsLatParam,
						new RowMapper<Map<String, String>>() {
							@Override
							public Map<String, String> mapRow(ResultSet rs,
									int index) throws SQLException {
								Map<String, String> map = new HashMap<String, String>();
								map.put("ID", rs.getString("ID"));
								map.put("DIMENSIONNAME",
										rs.getString("DIMENSIONNAME"));
								map.put("PARENTID", rs.getString("PARENTID"));
								map.put("MEMO", rs.getString("MEMO"));
								return map;
							}
						});
				if (kmsLatList != null && !kmsLatList.isEmpty()
						&& kmsLatList.size() > 0) {
					return getParentKmsLat(resultList, kmsLatList.get(0));
				} else {
					return resultList;
				}

			}
		} else {
			return resultList;
		}
	}
}
